﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HIWSystem.Common;
using HIWSystem.Models;

namespace HIWSystem.ViewModel
{
    public class PackingPrintParamConfigDescViewModel : ViewModelBase
    {
        #region 属性
        private ObservableCollection<PartNumberEntity> printConfigSource;

        public ObservableCollection<PartNumberEntity> PrintConfigSource
        {
            get { return printConfigSource; }
            set { printConfigSource = value; this.RaisePropertyChanged(() => PrintConfigSource); }
        }

        #endregion

        #region 命令
        private RelayCommand loadPageCmd;

        public RelayCommand LoadPageCmd
        {
            get
            {
                if (loadPageCmd == null)
                {
                    loadPageCmd = new RelayCommand(LoadPage);
                }
                return loadPageCmd;
            }
            set { loadPageCmd = value; }
        }
        private RelayCommand<PartNumberEntity> mouseDoubleClickCmd;

        public RelayCommand<PartNumberEntity> MouseDoubleClickCmd
        {
            get
            {
                if (mouseDoubleClickCmd == null)
                {
                    mouseDoubleClickCmd = new RelayCommand<PartNumberEntity>(MouseDoubleClickHandler);
                }
                return mouseDoubleClickCmd;
            }
            set { mouseDoubleClickCmd = value; }
        }
        private RelayCommand<DataGrid> dataGridUnloaded;

        public RelayCommand<DataGrid> DataGridUnloaded
        {
            get
            {
                if (dataGridUnloaded == null)
                {
                    dataGridUnloaded = new RelayCommand<DataGrid>(DataGridUnloadedHandler);
                }
                return dataGridUnloaded;
            }
            set { dataGridUnloaded = value; }
        }


        #endregion

        #region 方法
        //页面加载
        private void LoadPage()
        {
            DataTable dt = new DataTable();
            PrintConfigSource = new ObservableCollection<PartNumberEntity>();
            dt = BusinessProcess.GetLabelPrintConfigInfo();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PrintConfigSource.Add(
                        new PartNumberEntity
                        {
                            LPCId = new LabelConfigEntity
                            {
                                Id = Convert.ToInt32(dt.Rows[i]["Id"].ToString()),
                                RowNum = Convert.ToInt32(dt.Rows[i]["RowNum"]==DBNull.Value?"0":dt.Rows[i]["RowNum"].ToString()??"0"),
                                TemplateName = dt.Rows[i]["TemplateName"].ToString(),
                                FormVariables = dt.Rows[i]["FormVariables"].ToString()
                            }
                        });
                }
            }
        }
        //双击GridData行事件处理函数
        private void MouseDoubleClickHandler(PartNumberEntity obj)
        {
            if (obj != null)
            {
                Messenger.Default.Send<int>(obj.LPCId.Id, "PackingPrintParamConfigDesc");
                ViewManager.Close("PackingPrintParamConfigDesc");
            }
        }
        /// <summary>
        /// 数据网格卸载事件
        /// 解决DataGrid中出现在AddNew或EditItem事务过程不允许Deferrefresh报错
        /// </summary>
        private void DataGridUnloadedHandler(DataGrid dg)
        {
            dg.CommitEdit(DataGridEditingUnit.Row, true);
        }
        #endregion
    }
}
